// $Id: ImportTables.cs 67 2008-05-07 21:07:39Z nvivo $

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBLGen.GUI.Forms
{
    public partial class ImportTables : Form
    {
        private Project _project;

        public ImportTables(Project project)
        {
            if (project == null)
                throw new ArgumentNullException("project");

            if (project.ImportPlugin == null)
                throw new ArgumentNullException("Project.ImportPlugin", "Project must have an import plugin.");

            _project = project;

            InitializeComponent();
        }

        private void ImportTables_Load(object sender, EventArgs e)
        {
            lblPluginName.Text = _project.ImportPlugin.Name;

            if (_project.ImportPlugin != null && _project.ImportPlugin.SettingsType != null)
                btnSettings.Enabled = true;
            else
                btnSettings.Enabled = false;
        }

        private void btnSettings_Click(object sender, EventArgs e)
        {
            PluginSettings settings = _project.ImportPlugin.CreateSettings();
            settings.LoadFrom(_project.SettingsManager);

            using (EditPluginSettings frm = new EditPluginSettings(_project.ImportPlugin, settings))
            {
                if (frm.ShowDialog() == DialogResult.OK)
                    frm.Settings.SaveTo(_project.SettingsManager);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            PluginSettings settings = _project.ImportPlugin.CreateSettings();
            
            if (settings != null)
                settings.LoadFrom(_project.SettingsManager);
            
            ImportPlugin plugin = (ImportPlugin) _project.ImportPlugin.CreatePlugin();
            plugin.Settings = settings;

            try
            {
                Table[] tables = plugin.GetAllTables();
                BuildTree(tables);
            }
            catch (Exception ex)
            {
                UnhandledException.ShowDialog(ex);
            }
        }

        private void BuildTree(Table[] tables)
        {
            TreeNode tablesNode = CreateNode("Tables", null);
            TreeNode viewsNode = CreateNode("Views", null);

            foreach (Table t in tables)
            {
                TreeNode node = CreateNode(t.QualifiedName, t);

                if (t.SourceType == TableSourceType.Table)
                    tablesNode.Nodes.Add(node);
                else if (t.SourceType == TableSourceType.View)
                    viewsNode.Nodes.Add(node);
            }

            treeTables.Nodes.Clear();
            treeTables.Nodes.Add(tablesNode);
            treeTables.Nodes.Add(viewsNode);

            tablesNode.Expand();
            viewsNode.Expand();
        }

        private TreeNode CreateNode(string text, object tag)
        {
            TreeNode node = new TreeNode(text);
            node.Tag = tag;

            return node;
        }

        private void treeTables_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;

            // check child nodes

            if (node.Nodes.Count > 0)
            {
                bool nodeChecked = node.Checked;

                foreach (TreeNode childNode in node.Nodes)
                    childNode.Checked = nodeChecked;
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            List<Table> selectedTables = new List<Table>();

            foreach (TreeNode folderNode in treeTables.Nodes)
            {
                foreach (TreeNode tableNode in folderNode.Nodes)
                {
                    if (tableNode.Checked)
                    {
                        Table t = (Table) tableNode.Tag;
                        selectedTables.Add(t);
                    }
                }
            }

            ImportBehavior behavior;

            if (rbReplace.Checked)
                behavior = ImportBehavior.Replace;
            else
                behavior = ImportBehavior.Merge;

            _project.Import(selectedTables, behavior);

            DialogResult = DialogResult.OK;
            Close();
        }
    }
}